package utils;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 数组工具类
 * @author bug-fsh
 */
public class ArrayUtils {

    /**
     * 数组是否空
     * @param objects
     * @return
     */
    public static boolean isEmpty(Object [] objects) {
        if (objects == null || objects.length == 0) return true;
        return false;
    }

    /**
     * 获取数组长度（为null数组长度设置为0）
     * @param objects
     * @return
     */
    public static int size(Object [] objects) {
        if (objects == null) return 0;
        return objects.length;
    }

    /**
     * 多个数组长度是否一致
     * 取数组第一个元素获取该长度，用该长度去类比下面数组元素，如果有不一样的话，就不一致
     * @param objects
     * @return
     */
    public static boolean isEqualOfSize(Object [] ... objects) {
        boolean result = true;
        if (!ArrayUtils.isEmpty(objects)) {
            int size = size(objects[0]);
            for (Object [] object : objects) {
                if (size(object) != size) {
                    result = false;
                    break;
                }
            }
        }
        return result;
    }


    public static <T> T[] addAll(T [] srcArr, T [] destArr) {
        if (isEmpty(srcArr) || isEmpty(destArr)) return srcArr;
        if (srcArr.getClass() != destArr.getClass()) return srcArr;
        return (T[])addAll((Object) srcArr, (Object) destArr);
    }

    private static Object addAll(Object src, Object dest) {
        int srcLength = Array.getLength(src);
        int destLength = Array.getLength(dest);
        Object result = Array.newInstance(src.getClass().getComponentType(), srcLength + destLength);
        System.arraycopy(src, 0, result, 0, srcLength);
        System.arraycopy(dest, 0, result, srcLength , destLength);
        return result;
    }

    public static void main(String [] args) {
        Integer [] src = {1,2};
        Integer [] dest = {1,3};
        Integer [] result = addAll(src, dest);
        System.out.println(Arrays.toString(result));
    }
}
